#include "act_delete.hpp"
#include "../action_constants.hpp"

using etcdserverpb::DeleteRangeRequest;

namespace etcdv3 {

AsyncDeleteAction::AsyncDeleteAction(const ActionParameters& param)
    : Action(param) {
    DeleteRangeRequest del_request;
    del_request.set_key(parameters.key);
    del_request.set_prev_kv(true);
    std::string range_end(parameters.key);
    if (parameters.withPrefix) {
        int ascii = (int)range_end[range_end.length() - 1];
        range_end.back() = ascii + 1;
        del_request.set_range_end(range_end);
    }

    context.set_deadline(parameters.gainDeadline());
    reader.reset(parameters.kv_stub->AsyncDeleteRange(&context, del_request, &cq_).release());

    reader->Finish(&reply, &status, (void*)this);
}

AsyncDeleteRangeResponse AsyncDeleteAction::ParseResponse() {
    AsyncDeleteRangeResponse del_resp;
    if (!status.ok()) {
        del_resp.set_error_code(status.error_code());
        del_resp.set_error_message(status.error_message());
    } else {
        del_resp.ParseResponse(parameters.key, parameters.withPrefix, reply);
    }
    return del_resp;
}

}
